/**
 * 
 */
package vn.edu.hungvuongaptech.controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import vn.edu.hungvuongaptech.common.Constant;
import vn.edu.hungvuongaptech.dao.TimeSheetDAO;
import vn.edu.hungvuongaptech.model.TimeSheetModel;

/**
 * @author ThanhHQ
 *
 */
public class ConfirmTimeSheetDeletionController extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, 
	 * javax.servlet.http.HttpServletResponse)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, 
	 * javax.servlet.http.HttpServletResponse)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String[] ids = request.getParameterValues("chkTimeSheetID");
		if (ids != null) {
			ArrayList<TimeSheetModel> timeSheetModels = new ArrayList<TimeSheetModel>();
			for (int i = 0; i < ids.length; ++i) {
				timeSheetModels.add(TimeSheetDAO.getTimeSheetById(Long.valueOf(ids[i])));
			}
			request.setAttribute("timeSheetModels", timeSheetModels);
			RequestDispatcher dispatcher = request.getRequestDispatcher("ConfirmTimeSheetDeletion.jsp");
			dispatcher.forward(request, response);
		} else {
			int roleId = Integer.parseInt(request.getSession().getAttribute(Constant.ROLE_ATT).toString());
			String nextPath = null;
			switch (roleId) {
			case 1: //Staff role
				nextPath = Constant.PATH_RES.getString("timesheet.staff.messagePath") + "?msg=Please selects timesheet's record to remove!";
				break;
			case 2: //Manager role
				nextPath = Constant.PATH_RES.getString("timesheet.manager.messagePath") + "?msg=Please selects timesheet's record to remove!";
				break;
			case 3: //Admin role
				nextPath = Constant.PATH_RES.getString("timesheet.admin.messagePath") + "?msg=Please selects timesheet's record to remove!";
				break;
			}
			response.sendRedirect(nextPath);
		}
	}
}
